/*
 * Copyright (C) 2012 Caldia Apps.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.caldia.android.frameworkG1.DB;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * オプションデータベース
 * 
 * @author k-matsuda@caldia.org
 * 
 */
public class FifteenColumnsDb extends AbstractSQLiteOpenHelper {

	/**
	 * テーブル名
	 */
	private static final String TABLE_NAME = "tbl_option";

	/**
	 * バージョン
	 */
	private static final int VERSION = 1;

	/**
	 * テーブル作成用SQL
	 */
	private static final String CREATE_SQL_V1;
	static {
		final StringBuilder sb = new StringBuilder();
		sb.append("CREATE TABLE " + TABLE_NAME + " (");
		sb.append(BaseColumns.RAWID
				+ " integer primary key autoincrement not null,");
		sb.append(BaseColumns.KEY + " text not null,");
		sb.append(BaseColumns.MIMETYPE + " text,");
		sb.append(BaseColumns.VALUE1 + " text ,");
		sb.append(BaseColumns.VALUE2 + " text ,");
		sb.append(BaseColumns.VALUE3 + " text ,");
		sb.append(BaseColumns.VALUE4 + " text ,");
		sb.append(BaseColumns.VALUE5 + " text ,");
		sb.append(BaseColumns.VALUE6 + " text ,");
		sb.append(BaseColumns.VALUE7 + " text ,");
		sb.append(BaseColumns.VALUE8 + " text ,");
		sb.append(BaseColumns.VALUE9 + " text ,");
		sb.append(BaseColumns.VALUE10 + " text ,");
		sb.append(BaseColumns.VALUE11 + " text ,");
		sb.append(BaseColumns.VALUE12 + " text ,");
		sb.append(BaseColumns.VALUE13 + " text ,");
		sb.append(BaseColumns.VALUE14 + " text ,");
		sb.append(BaseColumns.VALUE15 + " text ");
		sb.append(")");

		CREATE_SQL_V1 = sb.toString();
	}

	/**
	 * コンストラクタ
	 * 
	 * @param context
	 *            コンテキスト
	 * @param fileName
	 *            データベースファイル名
	 */
	public FifteenColumnsDb(final Context context, final String fileName) {

		super(context, fileName, VERSION);

	}

	@Override
	public void onCreate(final SQLiteDatabase db) {

		db.execSQL(CREATE_SQL_V1);

	}

	@Override
	public void onUpgrade(final SQLiteDatabase db, final int oldVersion,
			final int newVersion) {
		// TODO Auto-generated method stub

	}

	/**
	 * 挿入
	 * 
	 * @param values
	 *            挿入値
	 * @return 結果
	 */
	public long insert(final ContentValues values) {
		return getWritableDatabase().insert(TABLE_NAME, null, values);
	}

	/**
	 * 更新
	 * 
	 * @param values
	 *            更新値
	 * @param whereClause
	 *            検索条件
	 * @param whereArgs
	 *            検索値
	 * @return 更新数
	 */
	public int update(final ContentValues values, final String whereClause,
			final String[] whereArgs) {
		return getWritableDatabase().update(TABLE_NAME, values, whereClause,
				whereArgs);
	}

	/**
	 * 削除
	 * 
	 * @param whereClause
	 *            検索条件
	 * @param whereArgs
	 *            検索値
	 * @return 削除数
	 */
	public int delete(final String whereClause, final String[] whereArgs) {
		return getWritableDatabase().delete(TABLE_NAME, whereClause, whereArgs);
	}

	/**
	 * 検索
	 * 
	 * @param distinct
	 *            distinct
	 * @param columns
	 *            columns
	 * @param selection
	 *            selection
	 * @param selectionArgs
	 *            selectionArgs
	 * @param groupBy
	 *            groupBy
	 * @param having
	 *            having
	 * @param orderBy
	 *            orderBy
	 * @param limit
	 *            limit
	 * @return 検索結果
	 */
	public Cursor query(final boolean distinct, final String[] columns,
			final String selection, final String[] selectionArgs,
			final String groupBy, final String having, final String orderBy,
			final String limit) {
		return getReadableDatabase().query(distinct, TABLE_NAME, columns,
				selection, selectionArgs, groupBy, having, orderBy, limit);
	}
}
